package at.ac.univie.zsu.aguataplan.domain;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Version;
import javax.validation.constraints.Size;

/**
 * @author gerry
 * 
 */
@Entity
public class Club {

	@Id
	@GeneratedValue(strategy = GenerationType.TABLE)
	@Column
	private Long id;

	@Version
	@Column
	private Integer version;

	@Column(nullable = false, unique = true)
	@Size(min = 1)
	private String name;

	@OneToMany(cascade = CascadeType.ALL)
	private Set<Team> refTeam = new HashSet<Team>();

	@ManyToOne
	private ClubLocation refClubLocation;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<ClubEMail> refClubEMail = new HashSet<ClubEMail>();

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	private Set<ClubWeb> refClubWeb = new HashSet<ClubWeb>();

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public Club() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	public Club addTeam(Team team) {
		this.refTeam.add(team);
		return this;
	}

	public void deleteTeam(long id) {
		Iterator<Team> it = refTeam.iterator();
		while (it.hasNext()) {
			Team t = it.next();
			if (t.getId() == id) {
				it.remove();
				return;
			}
		}
	}

	public ClubEMail addEMail(String address) {
		ClubEMail clubEMail;
		clubEMail = new ClubEMail(address);
		clubEMail.setRefClub(this);

		refClubEMail.add(clubEMail);
		return clubEMail;
	}

	public void deleteEMail(long id) {
		Iterator<ClubEMail> it = refClubEMail.iterator();
		while (it.hasNext()) {
			ClubEMail m = it.next();
			if (m.getId() == id) {
				it.remove();
				return;
			}
		}
	}

	public ClubWeb addWeb(String address) {
		ClubWeb clubWeb;
		clubWeb = new ClubWeb(address);
		clubWeb.setRefClub(this);

		refClubWeb.add(clubWeb);
		return clubWeb;
	}

	public void deleteWeb(long id) {
		Iterator<ClubWeb> it = refClubWeb.iterator();
		while (it.hasNext()) {
			ClubWeb w = it.next();
			if (w.getId() == id) {
				it.remove();
				return;
			}
		}
	}

	public String toString() {
		return this.name;
	}

	public String toStringExtended() {
		StringBuilder sb = new StringBuilder();
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("Name: ").append(getName()).append(", ");
		sb.append("RefClubEMail: ").append(getRefClubEMail() == null ? "null" : getRefClubEMail().size()).append(", ");
		sb.append("RefClubLocation: ").append(getRefClubLocation()).append(", ");
		sb.append("RefClubWeb: ").append(getRefClubWeb() == null ? "null" : getRefClubWeb().size()).append(", ");
		sb.append("RefTeam: ").append(getRefTeam() == null ? "null" : getRefTeam().size()).append(", ");
		sb.append("Version: ").append(getVersion());
		return sb.toString();
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Integer getVersion() {
		return version;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Team> getRefTeam() {
		return refTeam;
	}

	public void setRefTeam(Set<Team> refTeam) {
		this.refTeam = refTeam;
	}

	public ClubLocation getRefClubLocation() {
		return refClubLocation;
	}

	public void setRefClubLocation(ClubLocation refClubLocation) {
		this.refClubLocation = refClubLocation;
	}

	public Set<ClubEMail> getRefClubEMail() {
		return refClubEMail;
	}

	public void setRefClubEMail(Set<ClubEMail> refClubEMail) {
		this.refClubEMail = refClubEMail;
	}

	public Set<ClubWeb> getRefClubWeb() {
		return refClubWeb;
	}

	public void setRefClubWeb(Set<ClubWeb> refClubWeb) {
		this.refClubWeb = refClubWeb;
	}

}
